package com.hedl.classroom.controller;

import com.hedl.classroom.base.exception.CommonResult;
import com.hedl.classroom.base.exception.ValidationGroups;
import com.hedl.classroom.base.model.PageParams;
import com.hedl.classroom.base.model.PageResult;
import com.hedl.classroom.model.dto.AddCourseDto;
import com.hedl.classroom.model.dto.CourseBaseInfoDto;
import com.hedl.classroom.model.dto.EditCourseDto;
import com.hedl.classroom.model.dto.QueryCourseParamsDto;
import com.hedl.classroom.model.po.CourseBase;
import com.hedl.classroom.service.CourseBaseInfoService;
import com.hedl.classroom.utils.SecurityUtils;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@RestController
@Api(value = "课程信息管理接口",tags = {"课程信息管理接口"})
public class CourseBaseInfoController {

    //课程信息管理接口
    @Resource
    @ApiModelProperty(value = "课程信息管理接口",dataType = "Object")
    private CourseBaseInfoService courseBaseInfoService;

    @PreAuthorize("hasAuthority('xc_teachmanager_course_list')")  //指定权限标识符,拥有此权限才可以访问此方法
    @PostMapping("/course/list")
    @ApiOperation(value = "课程查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "起始页",required = true,dataType = "long"),
            @ApiImplicitParam(name = "pageSize",value = "结束页",required = true,dataType = "long"),
            @ApiImplicitParam(name = "queryCourseParamsDto",value = "条件查询",required = false,dataType = "Object")
    })
    public PageResult<CourseBase> list(PageParams pageParams,
                                       @RequestBody(required = false) QueryCourseParamsDto queryCourseParamsDto){
        //当前登陆用户
        SecurityUtils.XcUser user = SecurityUtils.getUser();
        //用户所属的机构id
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        PageResult<CourseBase> courseBasePageResult = courseBaseInfoService.queryCourseBaseList(companyId,pageParams, queryCourseParamsDto);

        //log.info("分页数据是：{}",courseBasePageResult);
        return courseBasePageResult;
    }

    @PostMapping("/course")
    @ApiOperation("新增课程")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseDto",value = "新增课程",required = true,dataType = "Object")
    })
    public CourseBaseInfoDto createCourseBase(@RequestBody @Validated(ValidationGroups.Inster.class) AddCourseDto courseDto){
        //int a = 10/0;
        //当前登陆用户
        SecurityUtils.XcUser user = SecurityUtils.getUser();
        //用户所属的机构id
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        //机构id，由于认证系统没有上线展示硬编码
        CourseBaseInfoDto courseBase = courseBaseInfoService.createCourseBase(companyId, courseDto);
        return courseBase;
    }

    @GetMapping("course/{id}")
    @ApiOperation("根据课程id查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "课程id",required = true,dataType = "Long")
    })
    public CourseBaseInfoDto queryById(@PathVariable("id") Long id){

        //获取当前用户的身份
//        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        System.out.println("用户身份信息："+principal);
        SecurityUtils.XcUser user = SecurityUtils.getUser();
        System.out.println("用户身份信息:"+user);
        System.out.println(user.getUsername());

        log.info("查询id是：{}",id);
        return courseBaseInfoService.queryById(id);
    }

    @PutMapping("/course")
    @ApiOperation("修改课程营销信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "editCourseDto",value = "修改课程营销数据",required = true,dataType = "Object")
    })
    public CourseBaseInfoDto updateCourseBaseAndCourseMarket(@RequestBody @Validated(value = ValidationGroups.Update.class) EditCourseDto editCourseDto){
        log.info("修改的课程营销信息如下：{}",editCourseDto);
        //Long companyId = 12345678901L;
        SecurityUtils.XcUser user = SecurityUtils.getUser();
        String companyId = user.getCompanyId();
        return courseBaseInfoService.updateCourseBaseAndCourseMarket(Long.parseLong(companyId),editCourseDto);
    }

    @DeleteMapping("/course/{courseId}")
    @ApiOperation("删除课程/课程营销信息/课程计划/任课老师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId",value = "课程id",required = true,dataType = "Long")
    })
    public CommonResult deleteCourseBase(@PathVariable("courseId") Long courseId){

        //当前登陆用户
        SecurityUtils.XcUser user = SecurityUtils.getUser();
        //用户所属的机构id
        Long companyId = null;
        if (StringUtils.isNotEmpty(user.getCompanyId())){
            companyId = Long.parseLong(user.getCompanyId());
        }
        return courseBaseInfoService.deleteCourseBase(courseId,companyId);
    }

}
